package com.ces.knacky.restws.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ces.knacky.framework.controller.ListController;
import com.ces.knacky.framework.dao.HibernateDAO;
import com.ces.knacky.model.Item;



@Path("catalogService")
@Component
public class CatalogRService extends ListController<Item> {
	
	@Context
	UriInfo uriInfo;
	
	@Context
	Request request;
	
	
	
	@GET
	@Produces({"application/xml","application/json"})
	public List<Item> getAllItems()
	{
		//List<Item> loi = new ArrayList<Item>();
		
		List<Item> loi;
		
		loi = (List<Item>) commonService.list("SELECT item from Item item");
		
		return loi;
		
	}
	
	@GET
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	@Path("{id}")
	public List<Item> getItem(@PathParam("id") int id)
	{
		
		return (List<Item>) commonService.list("SELECT item from Item item where item.id = " + id);
			
	}
	
	@POST
	@Consumes({"application/json", "application/xml"})
	@Produces("text/plain")
	@Path("/add")
	public String addItem(Item item)
	{
		
		System.out.println("Add:" + item.getTitle());
		
		if(commonService.add(item) == false)
		{
			System.out.println("Error in adding");
		}
		
		return "Item added";
	
	}
	
	/*@POST
	@Consumes("application/json")
	@Produces("text/plain")
	@Path("/addj")
	public String addJsonItem(Item item)
	{
		
		System.out.println("Add Json: " + item);
		if(commonService.add(item) == false)
		{
			System.out.println("Error in adding");
		}
		return "Item added";
	
	}*/
	
	
	
	@PUT
	@Consumes({"application/json", "application/xml"})
	@Produces("text/plain")
	@Path("/update")
	public String updateItem(Item item)
	{
		System.out.println("Update: " + item);
		if(commonService.update(item) == false)
		{
			System.out.println("Error in updating");
		}
		return "Item updated";
	
	}
	
	@DELETE
	@Consumes("application/xml")
	@Produces("text/plain")
	@Path("{id}")
	public String deleteItem(@PathParam("id") int id)
	{
		Item item = (Item) commonService.load(Item.class, id);
		
		System.out.println("Deleted Id: " + id);
		if(commonService.delete(item) == false)
		{
			System.out.println("Error in deleting");
		}
		return "Item deleted";
	
	}
	
	

}
